unit clsDictionary;

interface

Uses
  Classes;

Type

  TDictionary = class
  private
    FValue: String;
    FAlias: String;
    procedure SetAlias(const Value: String);
    procedure SetValue(const Value: String);
  public
    property Value: String read FValue write SetValue;
    property Alias: String read FAlias write SetAlias;
    constructor Create(AAlias, AValue: String);
  End;

  { Adapter / Wrapper }
  TDictionaryList = Class
  private
    FList: TList;
    function GetItems(Index: Integer): TDictionary;
    procedure SetItems(Index: Integer; const Value: TDictionary);
    function IndexOf(AAlias: String): Integer;
    function GetNames(Alias: String): String;
    function Count: Integer;
    property Items[Index: Integer]: TDictionary read GetItems write SetItems;
  public
    procedure Add(AAlias, AValue: String);
    destructor Destroy; override;
    property Names[Alias: String]: String read GetNames; default;
    constructor Create;
  End;

implementation

uses
  SysUtils;

{ TDictionary }

constructor TDictionary.Create(AAlias, AValue: String);
begin
  inherited Create;
  FAlias := AAlias;
  FValue := AValue;
end;

procedure TDictionary.SetAlias(const Value: String);
begin
  FAlias := Value;
end;

procedure TDictionary.SetValue(const Value: String);
begin
  FValue := Value;
end;

{ TDictionaryList }

procedure TDictionaryList.Add(AAlias, AValue: String);
begin
  try
    FList.Add(TDictionary.Create(AAlias, AValue));
  except
    on e: Exception do
    begin
      raise Exception.Create('Erro ao adicionar um Dictionary.' + #13 +
        'Alias: ' + QuotedStr(AAlias) + '; ' + 'FieldName: ' + QuotedStr(AValue)
        + #13 + 'Local: DictionaryList::Add');
    end;
  end;
end;

function TDictionaryList.Count: Integer;
begin
  Result := FList.Count;
end;

constructor TDictionaryList.Create;
begin
  FList := TList.Create;
  inherited Create;
end;

destructor TDictionaryList.Destroy;
begin
  FList.Destroy;
  inherited;
end;

function TDictionaryList.GetItems(Index: Integer): TDictionary;
begin
  Result := TDictionary(FList[Index]);
end;

function TDictionaryList.GetNames(Alias: String): String;
var
  Index: Integer;
begin
  Result := '';
  Index := Self.IndexOf(Alias);
  if Index <> -1 then
  begin
    Result := Self.Items[Index].FValue;
  end;
end;

function TDictionaryList.IndexOf(AAlias: String): Integer;
var
  Index: Integer;
begin
  Result := -1;
  for Index := 0 to Self.Count - 1 do
  begin
    if UpperCase(Self.Items[Index].FAlias) = UpperCase(AAlias) then
    begin
      Result := Index;
      Break;
    end;
  end;
end;

procedure TDictionaryList.SetItems(Index: Integer; const Value: TDictionary);
begin
  FList[Index] := Value;
end;

end.
